package heven.holt.wan.android.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import heven.holt.wan.android.databinding.ItemCoinRecordBinding
import heven.holt.wan.android.model.CoinRecordModel

class CoinRecordAdapter :
    PagingDataAdapter<CoinRecordModel, CoinRecordAdapter.CoinRecordViewHolder>(CoinRecordComparator()) {

    override fun onBindViewHolder(holder: CoinRecordViewHolder, position: Int) {
        holder.binding.coinRecord = getItem(position)
        holder.binding.executePendingBindings()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CoinRecordViewHolder =
        CoinRecordViewHolder(
            ItemCoinRecordBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )

    class CoinRecordViewHolder(
        val binding: ItemCoinRecordBinding
    ) : RecyclerView.ViewHolder(binding.root)

    class CoinRecordComparator : DiffUtil.ItemCallback<CoinRecordModel>() {
        override fun areItemsTheSame(oldItem: CoinRecordModel, newItem: CoinRecordModel): Boolean =
            oldItem.id == newItem.id

        override fun areContentsTheSame(
            oldItem: CoinRecordModel,
            newItem: CoinRecordModel
        ): Boolean = oldItem == newItem
    }
}